﻿using HCQ2_Common;
using HCQ2_Model;
using HCQ2_Model.ViewModel;
using HCQ2_Model.VLine.InputDto._0416;
using HCQ2_Model.VLine.Params;
using HCQ2_Model.VLine.Result;
using HCQ2_Model.VR.Params;
using HCQ2_Model.VRDD.InputDto._0531;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HCQ2UI_Logic.DocumentController
{
    /// <summary>
    ///  度度圈：关键字管理、消息推送
    /// </summary>
    public class VRSearchKeywordController : BaseLogic
    {
        //***************************1.0 关键字管理*******************************
        #region 1.0 默认首次进入关键字管理界面 + ActionResult KeywordList()
        /// <summary>
        ///  默认首次进入关键字管理界面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Load]
        [HCQ2_Common.Attributes.Element]
        [HttpGet]
        public ActionResult KeywordList()
        {
            return View();
        }
        #endregion

        #region 1.1 初始化Table + ActionResult InitTable()
        /// <summary>
        ///  初始化Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitApplyTable(KeywordParam param)
        {
            int total;
            var list = operateContext.bllSession.T_VRDDKeyword.GetKeywordList(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 1.1 添加关键字 + ActionResult AddKeyword(AddKeywordParam param)
        /// <summary>
        ///  添加关键字
        /// </summary>
        /// <param name="param">关键词对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddKeyword(AddKeywordParam param)
        {
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, getErrorMessage(ModelState), "", "");
            //添加之前判断产品类别、关键词是否已添加
            int mark = operateContext.bllSession.T_VRDDKeyword.SelectCount(s => s.pro_type == param.pro_type && s.keyword.Equals(param.keyword));
            if (mark > 0)
                return operateContext.RedirectAjax(1, "产品类别、关键词已添加！", "", "");
            //添加关键词
            T_VRDDKeyword obj = new T_VRDDKeyword
            {
                pro_type = param.pro_type,
                keyword = param.keyword,
                degree = param.degree,
                create_date = DateTime.Now,
                create_id = operateContext.Usr.user_id,
                create_name = operateContext.Usr.user_name
            };
            mark = operateContext.bllSession.T_VRDDKeyword.Add(obj);
            if (mark <= 0)
                return operateContext.RedirectAjax(1, "添加失败！", "", "");
            return operateContext.RedirectAjax(0, "添加成功~", "", "");
        }
        #endregion

        #region 1.2 编辑关键字 + ActionResult EditKeyword(EditKeywordParam param)
        /// <summary>
        ///  编辑关键字
        /// </summary>
        /// <param name="param">关键词对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditKeyword(EditKeywordParam param)
        {
            if (param.id <= 0)
                return operateContext.RedirectAjax(1, "关键词id异常！", "", "");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, getErrorMessage(ModelState), "", "");
            T_VRDDKeyword obj = new T_VRDDKeyword
            {
                pro_type = param.pro_type,
                keyword = param.keyword,
                degree = param.degree,
                update_id = operateContext.Usr.user_id,
                update_name = operateContext.Usr.user_name,
                update_date = DateTime.Now
            };
            //更新前判断修改后的产品类别、关键词是否已被添加
            int mark = operateContext.bllSession.T_VRDDKeyword.SelectCount(s => s.pro_type == param.pro_type && s.keyword.Equals(param.keyword) && s.id != param.id);
            if(mark>0)
                return operateContext.RedirectAjax(1, "编辑的产品类别、关键词已被添加！", "", "");
            mark = operateContext.bllSession.T_VRDDKeyword.Modify(obj, s => s.id == param.id, "pro_type", "keyword", "degree", "update_id", "update_name", "update_date");
            return operateContext.RedirectAjax(0, "修改关键词成功~", "", "");
        }
        #endregion

        #region 1.3 删除关键词 +ActionResult DelKeywordById(int id)
        /// <summary>
        ///  删除关键词
        /// </summary>
        /// <param name="id">关键词id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelKeywordById()
        {
            string ids = RequestHelper.GetStrByName("ids");
            if(string.IsNullOrEmpty(ids))
                return operateContext.RedirectAjax(1, "未选择待删除的记录行~", "", "");
            string[] item = ids.Split(',');
            foreach(string id in item){
                int k_id = Helper.ToInt(id);
                operateContext.bllSession.T_VRDDKeyword.Delete(s => s.id == k_id);
            }
            return operateContext.RedirectAjax(0, "数据删除成功~", "", "");
        }
        #endregion

        //***************************2.0 消息推送*******************************
        #region 2.0 默认首次进入消息推送管理界面 + ActionResult MessList()
        /// <summary>
        ///  默认首次进入消息推送管理界面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Load]
        [HCQ2_Common.Attributes.Element]
        [HttpGet]
        public ActionResult MessList()
        {
            return View();
        }
        #endregion

        #region 2.1 初始化Table + ActionResult InitMessTable(MessTableParam param)
        /// <summary>
        ///  初始化Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitMessTable(MessTableParam param)
        {
            int total;
            var list = operateContext.bllSession.T_VRDDKeyword.GetMessTableList(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        //***************************3.0 用户推送*******************************
        #region 3.0 默认首次进入用户推送管理界面 + ActionResult MessUserList()
        /// <summary>
        ///  默认首次进入用户推送管理界面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Load]
        [HCQ2_Common.Attributes.Element]
        [HttpGet]
        public ActionResult MessUserList()
        {
            return View();
        }
        #endregion

        #region 3.1 初始化消息用户Table + ActionResult InitMessUserTable()
        /// <summary>
        ///  初始化消息用户Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitMessUserTable(MessUserTableParam param)
        {
            int total;
            var list = operateContext.bllSession.T_VRDDKeyword.GetMessUserTableList(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}